package com.sunriz.motordrive.utils;

import com.sunriz.motordrive.server.common.impl.BaseSerialImpl;
import jssc.SerialPortException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import static com.sunriz.motordrive.constant.DriveParameters.DATA_SIZE;

/**
 * ClassName: FourChannelDataImpl
 * Package: com.sunriz.motordrive.server.impl
 * Description:
 *
 * @Author BuTang
 * @Create 2024/4/29 11:00
 * @Version 1.0
 */
@Slf4j
@Service
public class FourChannelDataImpl {
    private static ArrayList<Integer> list1 = new ArrayList<Integer>(), list2 = new ArrayList<Integer>(),list3 = new ArrayList<Integer>(),list4 = new ArrayList<Integer>();
    private static void processingValue(ArrayList<Integer> list){

        log.info("进行消息处理，list长度：{}",list.size());
        // 排序
//        Collections.sort(list);
        // 删除最大及最小值的各三个值
        if (list.size()>=DATA_SIZE && !list.isEmpty()) {
//        if (list.size()>=3 && !list.isEmpty()) {

            boolean flag = true;
//            list.remove(0);
//            list.remove(1);
//            list.remove(2);
//            list.remove(list.size()-1);
//            list.remove(list.size()-2);
//            list.remove(list.size()-3);

            double avg ,add =0 ;
            double num = 0 , upLeft , downLeft , upsurge = 0 , downWave = 0 ;

            // 取得平均值
//            avg = list.stream()
//                    .mapToInt(Integer::intValue)
//                    .average().
//                    orElse(Double.NaN);

            for (int i = 0; i < list.size(); i++) {
                add += list.get(i);
            }
            avg = add /list.size();

            if (list.get(0) > avg){
                upLeft = list.get(0);
                downLeft = list.get(1);
            }else {
                upLeft = list.get(1);
                downLeft =  list.get(0);
            }

            // 对于数组进行遍历，获取波动值的和;
            for (Integer value : list) {

                if (value != null){
                    if (value>avg){
                        if (flag == true){
                            flag = false ;
                            upLeft = value;
                        }
                        upsurge += Math.abs (value - upLeft);
                        upLeft = value;
                    }else {
                        downWave += Math.abs (value - downLeft);
                        downLeft = value;
                    }
                }

            }

            // 取得平均值
            upsurge = upsurge / (list.size()/2 - 1) ;
            downWave = downWave / (list.size()/2 - 1) ;

            System.out.println("avg value :"+avg);
            System.out.println("upsurge value :"+upsurge);
            System.out.println("downWave value :"+downWave);

            // 将list的长度置空
            log.info("消息处理结束，list长度：{}",list.size());
            list.clear();
        }
    }




    /**
     * 处理报文
     * @param data
     */
    private void processData(String data){
        try {
            String[] split = data.split(" ");
            int index = 0;
            for (int i = 0; i < split.length; i++) {
                int value = Integer.parseInt(split[2*i] + split[(2 * i + 1)], 16);

                index = i % 4;
                switch (index){
                    case 0:
                        list1.add(value);
                        break;
                    case 1:
                        list2.add(value);
                        break;
                    case 2:
                        list3.add(value);
                        break;
                    case 3:
                        list4.add(value);
                        break;
                    default:
                }
//                System.out.println(value);
            }
        } catch (ArrayIndexOutOfBoundsException e) {
//            System.out.println("出现空指针异常");
        }
    }
    private static void sendMessage(String name,String value) {
        String data = name+":"+value;
        byte[] bytes = data.getBytes();
        try {
            BaseSerialImpl.serialPort.writeBytes(bytes);
        } catch (SerialPortException e) {
            log.info("出现故障");
            e.printStackTrace();
        }
    }

}
